/****************************************************************************/
/* NAME:           Hal_InputManagement.c                                    */
/* PURPOSE:        Manage all basic input and output                        */
/* CREATED_BY:     Vyx                                                      */
/* CREATION_DATE:  03/07/2012                                               */
/************************************** (C) Copyright 2012 AquaLink *********/

#include "Hal_InputManagement.h"
#include "Hal_InputManagement_Config.h"
#include "stdio.h"

#define INPUT_NUMBER         (sizeof(InputConfigList) / sizeof(InputConfigList[0]))
#define INPUT_TASK_DELAY     (50) /* 50ms */
#define INPUT_SAMPLE_NUMBER  (3)

bool InputFiltered[INPUT_NUMBER];

/**
  * @brief  Main function of input management.
  *         This function is execute one time at the start.
  * @param  none
  * @retval None
  */
void vTaskCTRL_InputManagementInit(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  uint8_t          i;

  /* init all input pin */
  for (i=0; i<INPUT_NUMBER; i++)
  {
    /* start clock gpio if is needed */
    if (InputConfigList[i].InputGpio == GPIO_A)
    {
       RCC_APB2PeriphClockCmd(CLOCK_GPIOA ,ENABLE);
    }
    if (InputConfigList[i].InputGpio == GPIO_B)
    {
       RCC_APB2PeriphClockCmd(CLOCK_GPIOB ,ENABLE);
    }
    if (InputConfigList[i].InputGpio == GPIO_C)
    {
       RCC_APB2PeriphClockCmd(CLOCK_GPIOC ,ENABLE);
    }
    if (InputConfigList[i].InputGpio == GPIO_D)
    {
        RCC_APB2PeriphClockCmd(CLOCK_GPIOD ,ENABLE);
    }
    if (InputConfigList[i].InputGpio == GPIO_E)
    {
        RCC_APB2PeriphClockCmd(CLOCK_GPIOE ,ENABLE);
    }
    if (InputConfigList[i].InputGpio == GPIO_F)
    {
        RCC_APB2PeriphClockCmd(CLOCK_GPIOF ,ENABLE);
    }
    if (InputConfigList[i].InputGpio == GPIO_G)
    {
        RCC_APB2PeriphClockCmd(CLOCK_GPIOG ,ENABLE);
    }

    GPIO_InitStructure.GPIO_Pin  = InputConfigList[i].InputPin;
    GPIO_InitStructure.GPIO_Mode = InputConfigList[i].InputMode;
    GPIO_Init(InputConfigList[i].InputGpio, &GPIO_InitStructure);
  }
}

/**
  * @brief  Service to get filtered input
  * @param  none
  * @retval None
  */
bool INPUT_InputFilteredManagement(uint8_t InputName)
{
    portTickType xLastWakeTime;
    bool         InputHistory[INPUT_SAMPLE_NUMBER];
    bool         RetVal;
    uint8_t      Result;
    uint8_t      it;

    /* init */
    xLastWakeTime = xTaskGetTickCount();
    Result        = 0;

    for (it = 0; it < INPUT_SAMPLE_NUMBER; it++)
    {
        InputHistory[it] = INPUT_GetInputValue(InputName);

        wait(INPUT_TASK_DELAY);
    }

    for (it = 0; it < INPUT_SAMPLE_NUMBER; it++)
    {
       Result += InputHistory[it];
    }

    if (Result == INPUT_SAMPLE_NUMBER)
    {
        RetVal = TRUE;
    }
    else if (Result == 0)
    {
        RetVal = FALSE;
    }
    else
    {
        RetVal = FALSE;
    }

    return (RetVal);
}

/**
  * @brief  Get the value of selected input.
  * @param  Name of input
  * @retval Value of input (TRUE or FALSE)
  */
bool INPUT_GetInputValue(uint8_t InputName)
{
  return GPIO_ReadInputDataBit(InputConfigList[InputName].InputGpio,
                                InputConfigList[InputName].InputPin);
}
